#!/bin/bash
# 基于 cmake 的 thrift 编译脚本(支持交叉编译)
# Optional Environment Variables: 
#        BUILD_TYPE 编译版本类型(DEBUG|RELEASE),默认 RELEASE
#        TOOLCHAIN_FILE 指定交叉编译的工具链文件
#        PREFIX 安装路径
#        BOOST_ROOT boost 位置
#        BUILD_COMPILER 是否编译thrft compiler(ON|OFF),默认 ON
#        THRIFT_FOLDER thrift 源码路径,默认 thrift or thrift-0.11.0
#        PROJECT_FOLDER cmake 生成的工程文件(Makefile)文件夹
#        WITH_OPENSSL 是否支持 openssl (ON|OFF),默认 ON
#        WITH_ZLIB 是否 zlib 压缩 (ON|OFF),默认 OFF
#        WITH_BOOST_STATIC 是否使用boost 静态库(ON|OFF),默认 ON
#        THRIFT_ADDITIONAL_OPTS 额外的选项
#        NO_LINKE_GCC_STATIC 非静态链接库 libstdc++库

# 安装对应平台的依赖库
# sudo apt-get install bison flex g++ gcc libboost-all-dev libssl-dev
#字符串转大写
#参数：1字符串
function string_toupper()
{
    echo $1 | tr '[a-z]' '[A-Z]'
}
sh_folder=$(cd "$(dirname $0)"; pwd -P)
# 定义编译的版本类型(DEBUG|RELEASE)
build_type=RELEASE
[[ "$(string_toupper $BUILD_TYPE)" =~ DEBUG|RELEASE ]] && build_type=$(string_toupper $BUILD_TYPE)

echo build_type=$build_type

boost_root=
[ -n "$BOOST_ROOT" ] && boost_root="-DBOOST_ROOT=$BOOST_ROOT"

toolchain=
[ -n "$TOOLCHAIN_FILE" ] && toolchain="-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE"

link_gcc_static="$CXXFLAGS -static-libgcc -static-libstdc++"

[ "$(string_toupper $NO_LINKE_GCC_STATIC)" = ON ] && link_gcc_static="$CXXFLAGS"

# 交叉编译时不指定连接静态gcc库
[ -n "$TOOLCHAIN_FILE" ] && link_gcc_static="$CXXFLAGS"

[ -z "$BUILD_COMPILER" ] && BUILD_COMPILER=ON
[[ "$(string_toupper $BUILD_COMPILER)" =~ ON|OFF ]] && BUILD_COMPILER=$(string_toupper $BUILD_COMPILER)

[ -z "$WITH_BOOST_STATIC" ] && WITH_BOOST_STATIC=ON
[[ "$(string_toupper $WITH_BOOST_STATIC)" =~ ON|OFF ]] && WITH_BOOST_STATIC=$(string_toupper $WITH_BOOST_STATIC)

# 交叉编译时,WITH_OPENSSL 默认为 OFF 
[ -z "$WITH_OPENSSL" ] && [ -n "$TOOLCHAIN_FILE" ] && WITH_OPENSSL=OFF
# 非交叉编译时,WITH_OPENSSL 默认为 ON
[ -z "$WITH_OPENSSL" ] && WITH_OPENSSL=ON

[[ "$(string_toupper $WITH_OPENSSL)" =~ ON|OFF ]] && WITH_OPENSSL=$(string_toupper $WITH_OPENSSL)

[ -n "$PREFIX" ] && install_prefix="$PREFIX"
[ -z "$PREFIX" ] && install_prefix=$sh_folder/dist/thrift-$(g++ -dumpmachine)
[ -e "$install_prefix" ] && rm -fr "$install_prefix"

[ -n "$THRIFT_FOLDER" ] && [ -d "$THRIFT_FOLDER" ] && thrift_folder=$THRIFT_FOLDER
[ -z "$THRIFT_FOLDER" ] && [ -d $sh_folder/thrift ] && thrift_folder=$sh_folder/thrift
[ -z "$thrift_folder" ] && echo "NOT DEFINED thrift_folder" && exit 255

[ -n "$PROJECT_FOLDER" ] && prj_folder="$PROJECT_FOLDER"
[ -z "$PROJECT_FOLDER" ] && prj_folder=$sh_folder/build/thrift-$(g++ -dumpmachine)

[ -z "$WITH_ZLIB" ] && WITH_ZLIB=ON
[[ "$(string_toupper $WITH_ZLIB)" =~ ON|OFF ]] && WITH_ZLIB=$(string_toupper $WITH_ZLIB)

zlib_root=
zlib_library=
[ -n "$ZLIB_ROOT" ] && zlib_root="-DZLIB_ROOT=$ZLIB_ROOT"
[ -n "$ZLIB_LIBRARY"  ] && zlib_library="-DZLIB_LIBRARY=$ZLIB_LIBRARY"

pushd $sh_folder

[ -d $prj_folder ] && rm -fr $prj_folder
mkdir -p $prj_folder || exit 255

pushd $prj_folder

CXXFLAGS="$link_gcc_static" \
cmake $thrift_folder -G "Unix Makefiles" \
  -DCMAKE_BUILD_TYPE=$build_type \
  -DWITH_BOOSTTHREADS=ON \
  -DWITH_C_GLIB=OFF \
  -DWITH_LIBEVENT=OFF \
  -DWITH_OPENSSL=$WITH_OPENSSL \
  -DWITH_SHARED_LIB=OFF \
  -DWITH_STATIC_LIB=ON \
  -DWITH_JAVA=OFF -DWITH_PYTHON=OFF -DWITH_HASKELL=OFF \
  -DCMAKE_INSTALL_PREFIX=$install_prefix \
  -DBUILD_TESTING=OFF \
  -DBUILD_TUTORIALS=OFF \
  -DWITH_QT4=OFF \
  -DWITH_ZLIB=$WITH_ZLIB \
  -DWITH_BOOST_STATIC=$WITH_BOOST_STATIC \
  -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
  -DBUILD_COMPILER=$BUILD_COMPILER \
  $boost_root \
  $toolchain \
  $zlib_root \
  $zlib_library \
  $THRIFT_ADDITIONAL_OPTS \
  || exit 255
   
cmake --build . --target install -- -j8

popd

popd
